import { createRouter, createWebHashHistory } from 'vue-router'
import { getCookie } from '@utils/jsCookie'
import { useUserStore } from '@/pinia/user'
import { getMyUserInfo } from '@api/person'

const routes = [
  {
    path: '/home',
    meta: {
      title: '首页'
    },
    component: () => import('./../layouts/default.vue'),
    children: [
      {
        path: 'index',
        component: () => import('./../views/tongji/home/index.vue'),
        meta: {
          title: '首页统计'
        }
      },
      {
        path: 'news',
        component: () => import('./../views/news/index.vue'),
        meta: {
          title: '文章列表'
        }
      },
      {
        path: 'news/detail',
        component: () => import('./../views/news/detail.vue'),
        meta: {
          title: '文章详情'
        }
      },
      {
        path: 'join',
        component: () => import('./../views/join/index.vue'),
        meta: {
          title: '招聘列表'
        }
      },
      {
        path: 'join/detail',
        component: () => import('./../views/join/detail.vue'),
        meta: {
          title: '招聘详情'
        }
      },
      {
        path: 'person',
        component: () => import('./../views/person/index.vue'),
        meta: {
          title: '员工列表'
        }
      },
      {
        path: 'customer',
        component: () => import('./../views/customer/index.vue'),
        meta: {
          title: '客服列表'
        }
      },
      {
        path: 'books',
        component: () => import('./../views/book/books/index.vue'),
        meta: {
          title: '书籍列表'
        }
      },
      {
        path: 'putong',
        component: () => import('./../views/book/putong/index.vue'),
        meta: {
          title: '普通书籍列表'
        }
      },
      {
        path: 'jingpin',
        component: () => import('./../views/book/jingpin/index.vue'),
        meta: {
          title: '精品书籍列表'
        }
      },
      {
        path: 'bookTypes',
        component: () => import('./../views/book/bookTypes/index.vue'),
        meta: {
          title: '书籍类型'
        }
      },
      {
        path: 'blackBooks',
        component: () => import('./../views/book/blackBooks/index.vue'),
        meta: {
          title: '黑名单书籍'
        }
      },
      {
        path: 'newBooks',
        component: () => import('./../views/book/newBooks/index.vue'),
        meta: {
          title: '新品单书籍'
        }
      },
      {
        path: 'getConfig',
        component: () => import('./../views/book/getConfig/index.vue'),
        meta: {
          title: '回收书籍规则'
        }
      },
      {
        path: 'order',
        component: () => import('./../views/order/orders/index.vue'),
        meta: {
          title: '订单列表'
        }
      },
      {
        path: 'orderDetail',
        component: () => import('./../views/order/orders/detail.vue'),
        meta: {
          title: '订单详情'
        }
      },
      {
        path: 'cangku',
        component: () => import('./../views/cangku/index.vue'),
        meta: {
          title: '仓库列表'
        }
      },
      {
        path: 'chongzhi',
        component: () => import('./../views/cangku/chongzhi.vue'),
        meta: {
          title: '仓库充值记录'
        }
      },
      {
        path: 'zhekou',
        component: () => import('./../views/book/zhekou/index.vue'),
        meta: {
          title: '折扣设置'
        }
      },
      {
        path: 'reason',
        component: () => import('./../views/book/reason/index.vue'),
        meta: {
          title: '理由列表'
        }
      },
      {
        path: 'addressKey',
        component: () => import('./../views/addressKey/index.vue'),
        meta: {
          title: '地址黑名单'
        }
      },
      {
        path: 'cityAddress',
        component: () => import('./../views/cityAddress/index.vue'),
        meta: {
          title: '城市快递优先级'
        }
      },
      {
        path: 'tjcangku',
        component: () => import('./../views/tongji/cangku/index.vue'),
        meta: {
          title: '仓库统计'
        }
      },
      {
        path: 'tixian',
        component: () => import('./../views/tixian/index.vue'),
        meta: {
          title: '提现列表'
        }
      },
      {
        path: 'yue',
        component: () => import('./../views/tixian/yue.vue'),
        meta: {
          title: '余额列表'
        }
      },
      {
        path: 'huiyuan',
        component: () => import('./../views/huiyuan/index.vue'),
        meta: {
          title: '会员列表'
        }
      },
      {
        path: 'qianbao',
        component: () => import('./../views/huiyuan/qianbao.vue'),
        meta: {
          title: '钱包明细'
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('./../views/login.vue'),
    meta: {
      title: '登录'
    },
    children: []
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  let userInfo = userStore.userInfo
  //更新用户信息更新权限
  getMyUserInfo().then((res) => {
    userStore.login(res.data)
  })

  if (to.path === '/login') {
    if (userInfo) {
      next({
        path: '/home/index'
      })
    } else {
      next()
    }
  } else {
    if (userInfo) {
      if (to.path === '/') {
        next({
          path: '/home/index'
        })
      } else {
        next()
      }
    } else {
      next({
        path: '/login'
      })
    }
  }
})

export default router
